package com.sairui.Xiaoruilib.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import cn.iyivi.R;

/**
 * Created by sunqiyong on 2018/6/30.
 */

public class WXBottomTouchView extends View {
    private Paint mSolidPaint;
    private int colorWhite;
    private int colorSolid;

    private Boolean isTouch = false;


    private Bitmap mBackGround;//中间的图片

    float mDownY = 0;//按下时手指的y坐标值
    boolean isCancel = false;//是否已经取消发送


    private TouchListener mTouchListener;
    private WxClickListener mWxClickListener;

    public void setWxClickListener(WxClickListener mWxClickListener) {
        this.mWxClickListener = mWxClickListener;
    }

    public WXBottomTouchView(Context context) {
        super(context);
    }

    public void setTouchListener(TouchListener mTouchListener) {
        this.mTouchListener = mTouchListener;
    }

    public WXBottomTouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        colorWhite = ContextCompat.getColor(context, R.color.time_color);
        colorSolid = ContextCompat.getColor(context, R.color.main_black);

        mSolidPaint = new Paint();
        mSolidPaint.setColor(colorWhite);

        mBackGround = BitmapFactory.decodeResource(getResources(),R.mipmap.ico_speak);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isTouch) {//"松开 结束";
            mSolidPaint.setColor(colorSolid);
        } else {//"按住 说话";
            mSolidPaint.setColor(colorWhite);
        }

        @SuppressLint("DrawAllocation")
        RectF r2 = new RectF(2, 2, getWidth() - 2, getHeight() - 2);
        canvas.drawRoundRect(r2, 20, 20, mSolidPaint);

        float middleX = getWidth() / 2;
        float middleY = getHeight() / 2;
        float picStartX = middleX - (mBackGround.getWidth()  / 2);
        float picStartY = middleY - (mBackGround.getHeight() / 2);
        canvas.drawBitmap(mBackGround, picStartX, picStartY, mSolidPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (mTouchListener == null) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchListener.start_recognize();
                mWxClickListener.click();
                mDownY = event.getY();
                isTouch = true;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                if (isCancel) {
                    break;
                }
                //上划取消
                if (mDownY - event.getY() > getHeight()) {
                    mTouchListener.cancel_recognize();
                    isCancel = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isCancel) {
                    isCancel = false;
                } else {
                    mTouchListener.end_recognize();
                }
                mTouchListener.actionUp();
                isTouch = false;
                invalidate();
                break;
        }
        return true;
    }

    public interface TouchListener {
        void start_recognize();//开始识别

        void cancel_recognize();//上滑取消

        void end_recognize();//手指抬起，结束识别，获取内容

        void actionUp();
    }

    public interface WxClickListener{
        void click();
    }

}
